<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="菜品名称">
              <a-input placeholder="请输入菜品名称" v-model="queryParam.cpName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="up_down_frame_status" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="菜品分类">
              <a-select
                mode="default"
                style="width: 100%"
                placeholder="请选择菜品分类"
                v-model="queryParam.categoryId">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(item, index) in cpCategoryList" :key="index" :value="item.categoryId">
                  {{ item.categoryName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="batchDel" type="primary" icon="plus">批量删除</a-button>
      <a-button @click="batchUp" type="primary" icon="plus">批量上架</a-button>
      <a-button @click="batchDown" type="primary" icon="plus">批量下架</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a @click="handleDetail(record)">详情</a>

          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <ord-cp-info-modal ref="modalForm" @ok="modalFormOk"></ord-cp-info-modal>
    <a-modal :maskClosable="false" v-model="visible" title="上架设置" @ok="handleOk">
      <a-form>
        <a-form-item label="上架方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-model="putType" placeholder="请选择上架方式">
            <a-select-option value="3">线下线上</a-select-option>
            <a-select-option value="1">线下</a-select-option>
            <a-select-option value="2">线上</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import OrdCpInfoModal from './modules/OrdCpInfoModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { deleteAction, getAction,postAction  } from '@api/manage'

export default {
  name: 'OrdCpInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    OrdCpInfoModal
  },
  data() {
    return {
      description: '菜品信息表管理页面',
      cpCategoryList: [],
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      queryParam:{
        cpType:"0"
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '图片',
          align: 'center',
          dataIndex: 'cpPicture',
          scopedSlots: { customRender: 'imgSlot' }
        },
        {
          title: '菜品分类',
          align: 'center',
          dataIndex: 'categoryName',
          customRender: function(text) {
            return !text ? '-' : (text.length > 0 ? text : '')
          }
        },
        {
          title: '菜品名称',
          align: 'center',
          dataIndex: 'cpName'
        },
        {
          title: '上架状态',
          align: 'center',
          dataIndex: 'status',
          customRender: function(text) {
            return text == '0' ? '待上架' : '已上架'
          }
        },
        {
          title: '上架方式',
          align: 'center',
          dataIndex: 'putType',
          customRender: function(text) {
            //上架方式 0无 1店内 2线上 3店内与线上 4外卖
            if(text=='0'){
              return "待上架"
            }else if(text=='1'){
              return "店内"
            }else if(text=='2'){
              return "线上扫码"
            }else if(text=='3'){
              return "店内与线上扫码"
            }
          }
        },
        {
          title: '店内价格',
          align: 'center',
          dataIndex: 'cpPrice'
        },
        {
          title: '线上价格',
          align: 'center',
          dataIndex: 'cpLinePrice'
        },
        {
          title: '每天供应数量',
          align: 'center',
          dataIndex: 'cpQuantity'
        },
        {
          title: '当天剩余数量',
          align: 'center',
          dataIndex: 'residualQuantity'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/ord/ordCpInfo/list',
        delete: '/ord/ordCpInfo/delete',
        deleteBatch: '/ord/ordCpInfo/deleteBatch',
        batchUp: '/ord/ordCpInfo/upBatch',
        batchDown: '/ord/ordCpInfo/downBatch',
        exportXlsUrl: '/ord/ordCpInfo/exportXls',
        importExcelUrl: 'ord/ordCpInfo/importExcel'
      },
      dictOptions: {},
      putType: '3'
    }
  },
  created() {
    this.initialCpCategoryList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleAdd: function () {
      //菜品类型 0店内 1外卖
      this.$refs.modalForm.add("0");
      this.$refs.modalForm.title = "新增";
      this.$refs.modalForm.disableSubmit = false;
    },
    initialCpCategoryList() {
      let params = {}
      getAction('/ord/category/cpCategoryList', params).then((res) => {
        if (res.success) {
          this.cpCategoryList = res.result
        }
      })
    },
    initDictConfig() {
    },

    handleOk(e) {
      var ids = [];
      for (var a = 0; a < this.selectedRowKeys.length; a++) {
        ids.push(this.selectedRowKeys[a])
      }
      var that = this
      postAction(that.url.batchUp, { ids: ids,putType:this.putType }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()
        } else {
          that.$message.warning(res.message)
        }
      }).finally(() => {
        that.loading = false
        that.visible = false
      })

    },
    batchUp: function() {
      if (!this.url.batchUp) {
        this.$message.error('请设置url.batchUp属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        this.visible = true
      }
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
